package com.huhu.controller;

import com.huhu.entity.ArticleInfo;
import com.huhu.entity.CommentInfo;
import com.huhu.entity.UserInfo;
import com.huhu.entity.vo.CommentInfoVO;
import com.huhu.service.ArticleService;
import com.huhu.service.CommentService;
import com.huhu.util.AjaxResult;
import com.huhu.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private ArticleService articleService;

    /**
     * 获取评论列表
     * @param aid
     * @return
     */
    @RequestMapping("/list")
    public Object getCommentList(Long aid) {
        // 1. 参数效验
        if(aid==null || aid<=0) {
            return AjaxResult.fail(-1, "参数有误！");
        }
        // 2. 查询数据库
        List<CommentInfoVO> list = commentService.getList(aid);
        // 3. 就结果返回给前端
        return AjaxResult.success(list);
    }


    /**
     * 发表评论
     *
     * @param aid
     * @param content
     * @param request
     * @return
     */
    @RequestMapping("/add")
    public Object addComment(Long aid, String content, HttpServletRequest request) {
        // 1.参数校验
        if (aid == null || aid <= 0 || !StringUtils.hasLength(content)) {
            return AjaxResult.fail(-1, "非法参数！");
        }
        // 2.组装数据（将 uid 从session中获取出来）
        UserInfo userInfo = SessionUtil.getLoginUser(request);
        if (userInfo == null || userInfo.getUid() <= 0) {
            return AjaxResult.fail(-2, "请先登录");
        }
        CommentInfo commentInfo = new CommentInfo();
        commentInfo.setAid(aid);
        commentInfo.setUid(userInfo.getUid());
        commentInfo.setContent(content);
        commentInfo.setCreatetime(LocalDateTime.now());
        // 3.将评论的对象存入数据库
        boolean result = commentService.addComment(commentInfo);
        // 4.将数据执行的结果返回给前端
        return AjaxResult.success(result == true ? 1 : 0);
    }


    /**
     * 删除评论
     *
     * @param aid
     * @param cid
     * @return
     */
    @RequestMapping("/del")
    public Object delComment(Long aid, Long cid, HttpServletRequest request) {
        // 1.参数校验
        if (aid == null || aid <= 0 || cid == null || cid <= 0) {
            return AjaxResult.fail(-1, "非法参数！");
        }
        // 2.校验权限（判断当前文章是否属于当前登录用户写的）
        // 2.1获取当前登录用户
        UserInfo userInfo = SessionUtil.getLoginUser(request);
        if (userInfo == null || userInfo.getUid() <= 0) {
            return AjaxResult.fail(-2, "请先登录！");
        }
        // 2.2获取当前文章id
        ArticleInfo articleInfo = articleService.getDetailByAid(aid);
        if (articleInfo == null && articleInfo.getAid() <= 0) {
            return AjaxResult.fail(-3, "非法的文章id！");
        }
        // 2.3比较文章归属的用户和当前登录的用户是否一致
        if (userInfo.getUid() != articleInfo.getUid()) {
            return AjaxResult.fail(-4, "非法操作！");
        }
        // 3.删除评论（操作数据库）
        boolean result = commentService.deleteCommentById(cid);
        // 4.将结果返回给前端
        return AjaxResult.success(result == true ? 1 : 0);
    }
}
